package com.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.dao.QuestionDao;
import com.pojo.Question;
import com.pojo.Zixun;
import com.service.WendaService;
import com.service.ZixunService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 招生问答管理接口
 */
@RestController
@RequestMapping("/wenda")
public class WendaAction {

    /**
     * 分页查询问答
     * @param page
     * @param limit
     * @param question
     * @return
     */
    @Autowired
    private WendaService wendaService;
    @RequestMapping("/select")
    public ResponseBean selectPage(int page,int limit,String question, Integer status){
        IPage iPage =wendaService.getZixunPage(page, limit,question,status);
        return new ResponseBean(0, iPage.getTotal(), "成功", iPage.getRecords());
    }

    /**
     * 添加招生问答
     * @param question
     * @return
     */
    @PostMapping("/add")
    public ResponseBean add(@RequestBody Question question){
        if(StringUtil.isNull(question.getQuestion())){
            return  ResponseBean.error("请填写咨询的问题");
        }
        if(StringUtil.isNull(question.getAnswen())){
            return  ResponseBean.error("请填写咨询问题的答案");
        }
        question.setStatus(1);
        boolean n = wendaService.save(question);
        if(n)
            return  ResponseBean.ok("添加成功");
        else
            return  ResponseBean.error("添加失败");
    }
    /**
     * 修改招生问答
     * @param question
     * @return
     */
    @PostMapping("/update")
    public ResponseBean update(@RequestBody Question question){
        boolean n = wendaService.updateById(question);
        if(n)
            return  ResponseBean.ok("修改成功");
        else
            return  ResponseBean.error("修改失败");
    }

    /**
     * 根据编号查询问答详情
     * @param qaid
     * @return
     */
    @RequestMapping("/getDetail")
    public  ResponseBean getDetail(String qaid){
        Question question = wendaService.getById(qaid);
        if(question!=null){
            return  new ResponseBean(200,"成功",question);
        }
        else{
            return  ResponseBean.error("查询失败");
        }
    }
    /**
     * 修改问答的状态
     * @param question
     * @return
     */
    @PostMapping("/updateStatus")
    public ResponseBean updateStatus(@RequestBody Question question){
        int n = question.getStatus();
        if(n==1)
            question.setStatus(0);
        else
            question.setStatus(1);
        boolean f = wendaService.updateById(question);
        if(f)
            return  ResponseBean.ok("修改成功");
        else
            return  ResponseBean.error("修改失败");
    }
}
